# buy_into.py
import json

from aaa.api.base_api import BaseAPI


def _handle_response_buy_into_fail_NoBankCard(response, action):
    try:
        response.encoding = 'utf-8'
        body = json.loads(response.text)
        print(f"{action}响应：{body}")
        if response.status_code == 200 and body.get('code') == 500:
            print(f"{action}成功")
            return True
        else:
            print(f"{action}失败：{body.get('msg', '未知错误')}")
            return False
    except json.JSONDecodeError:
        print(f"服务器未返回JSON格式，原始响应：{response.text}")
        return False


def _handle_response_buy_into_fail_NoPayPassword(response, action):
    try:
        response.encoding = 'utf-8'
        body = json.loads(response.text)
        print(f"{action}响应：{body}")
        if response.status_code == 200 and body.get('code') == 300:
            print(f"{action}成功")
            return True
        else:
            print(f"{action}失败：{body.get('msg', '未知错误')}")
            return False
    except json.JSONDecodeError:
        print(f"服务器未返回JSON格式，原始响应：{response.text}")
        return False


def _handle_response_NoMoney(response, action):
    try:
        response.encoding = 'utf-8'
        body = json.loads(response.text)
        print(f"{action}响应：{body}")
        if response.status_code == 200 and body.get('code') == 400:
            print(f"{action}成功")
            return True
        else:
            print(f"{action}失败：{body.get('msg', '未知错误')}")
            return False
    except json.JSONDecodeError:
        print(f"服务器未返回JSON格式，原始响应：{response.text}")
        return False


class BuyInto(BaseAPI):

    def buy_into(self, data_buy_into):
        url_buy_into = f'{self.BASE_URL}/user/userBuyChangeMoney'
        response = self.session.post(url_buy_into, data=data_buy_into)
        return self._handle_response(response, '买入')

    def buy_into_fail_NoPayPassword(self, data_buy_into):
        url_buy_into = f'{self.BASE_URL}/user/userBuyChangeMoney'
        response = self.session.post(url_buy_into, data=data_buy_into)
        return _handle_response_buy_into_fail_NoPayPassword(response, '无支付密码无法购买判断')

    def buy_into_fail_NoBankCard(self, data_buy_into):
        url_buy_into = f'{self.BASE_URL}/user/userBuyChangeMoney'
        response = self.session.post(url_buy_into, data=data_buy_into)
        return _handle_response_buy_into_fail_NoBankCard(response, '无绑卡无法购买判断')

    def buy_int_fail_NoMoney(self, data_buy_into):
        url_buy_into = f'{self.BASE_URL}/user/userBuyChangeMoney'
        response = self.session.post(url_buy_into, data=data_buy_into)
        return _handle_response_NoMoney(response, '无余额无法购买判断')





















